package com.atguigu.executor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 高并发 情况150个线程进来，
 16个线程回立即处理，
 会继续扩展16个线程继续处理，
 100个线程会进入到队列中去，
 剩余18个线程采用拒绝策略

 采用什么队列好些？
 LinkedBlockingDeque:不会引起空间碎片问题
 ArrayBlockingQueue: 会引起空间碎片问题

 */
@Configuration
@EnableConfigurationProperties(MyThreadPoolProperties.class)
public class MyThreadPoolConfig {
    @Autowired
     private MyThreadPoolProperties myProperties;
    @Bean
   public ThreadPoolExecutor myExecutor(){
       ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
               myProperties.getCorePoolSize(),
               myProperties.getMaximumPoolSize(),
               myProperties.getKeepAliveTime(),
               TimeUnit.SECONDS,
               new LinkedBlockingQueue(myProperties.getQueueLength()),
               Executors.defaultThreadFactory(),
               new ThreadPoolExecutor.CallerRunsPolicy());
       return threadPoolExecutor;
   }
}
